How is the RETAIN keyword considered for function block instances?
Concerning RETAIN
and NON_RETAIN
, the →variables of →function block instances are treated as the section VAR
in which the function block instance has been declared. This means:
-
If the keyword
RETAIN
is defined in sectionVAR
, the variables of the function block instance are treated as they would have been declared withRETAIN
. -
If the keyword
NON_RETAIN
is defined in sectionVAR
, the variables of the function block instance are treated as they would have been declared withNON_RETAIN
.
The following cases are treated as exceptions:
-
One or more variables are explicitly declared as
NON_RETAIN
orRETAIN
in the declaration of the appertaining function block. -
If function block instances are declared again in the declaration of the appertaining function block,
RETAIN
orNON_RETAIN
declared in the sectionVAR
of the first function block instance is not valid for their variables (but the one in the appertaining function block).
Here an example how the above rules are applied:
ST-code that is the basis for the following table
PROGRAM Test
VAR RETAIN
myFB1 : FB1;
var1 : INT;
END_VAR
END_PROGRAM
FUNCTION_BLOCK FB1
VAR
var2 : INT;
myFB2 : FB2;
myFB3 : FB3;
END_VAR
VAR NON_RETAIN
var3 : INT;
myFB3_2 : FB3;
END_VAR
VAR RETAIN
var7 : INT;
myFB3_3 : FB3;
END_VAR
END_FUNCTION_BLOCK
FUNCTION_BLOCK FB2
VAR
var4 : INT;
END_VAR
VAR NON_RETAIN
var5 : INT;
myFB3 : FB3;
END_VAR
END_FUNCTION_BLOCK
FUNCTION_BLOCK FB3
VAR
var6 : INT;
END_VAR
END_FUNCTION_BLOCK
The variable... |
is treated as ... |
Reason |
---|---|---|
|
|
keyword |
|
|
keyword |
|
|
keyword |
|
|
keyword |
|
|
no keyword in lines 9 and 25 |
|
|
keyword |
|
|
no keyword in lines 9 and 35 |
|
|
keyword |
|
|
keyword |
|
|
keyword |